# -*- coding:utf-8 -*-
import sqlconnect

def device_type(device_type):
    if device_type == 'Android':
        return '安卓设备'
    if device_type == 'headset':
        return '蓝牙耳机'
    if device_type == 'speaker':
        return '扬声器'
    if device_type == 'SV':
        return '蓝牙音响'
    if device_type == 'phone':
        return '苹果手机'
    if device_type == 'MACBook':
        return '苹果电脑'
    if device_type == 'PC':
        return 'PC'
    if device_type == 'iPad':
        return 'iPad'
    if device_type == 'Band':
        return '蓝牙手环'
    return '未知'


def deletebymac(mac):
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "delete from blue_info where asset_mac = %s;"
    cursor.execute(sql,[mac])
    db.commit()
    print "delete blue mac",mac
    cursor.close()
    db.close()
def deleteblueAttack(mac, attack_type):
    db = sqlconnect.connect()
    bluemac = db.cursor()
    sql = "delete from blueAttackScan where master = '%s' and attack_type = '%s'" %(mac,attack_type)
    bluemac.execute(sql)
    db.commit()
    print 'delete', mac,attack_type
    db.close()
# 在获取扫描信息之前，把所有的蓝牙设备在线情况设为离线
def updatestatus():
    db = sqlconnect.connect()
    bluemac = db.cursor()
    sql = "update blue_info set status=0"
    bluemac.execute(sql)
    db.commit()
    db.close()
# 根据经典蓝牙Mac获取攻击检测信息
def blueAttackByMac(mac):
    db = sqlconnect.connect()
    cur = db.cursor()
    sql = " select count(*) from blueAttackScan where master = %s order by scan_time desc"
    # print sql
    cur.execute(sql, [mac])
    count = cur.fetchall()
    #print count
    db.close()
    return count
# 根据低功耗蓝牙Mac获取攻击检测信息
def bleAttackByMac(mac):
    db = sqlconnect.connect()
    cur = db.cursor()
    sql = "select count(*) from bleAttackScan where slave = %s order by scan_time desc"
    # print sql
    cur.execute(sql, [mac.lower()])
    count = cur.fetchall()
    print count
    db.close()
    return count
#  统计设备存在的漏洞数
def bluebugcount(mac):
    db = sqlconnect.connect()
    cur = db.cursor()
    sql = "select count(*) from b_leak_m where asset_mac = %s"
    cur.execute(sql,[mac])
    count = cur.fetchall()
    db.close()
    return count
def getblueleakmessage(cve):
    db = sqlconnect.connect()
    cur = db.cursor()
    sql = "select leak_name,description,rank from leaklibrary where cve = %s"
    #print cve
    cur.execute(sql, [cve])
    count = cur.fetchall()
    if count != None:
        #print count
        db.close()
        return count
def deleteblueleak(mac,b_meassge):
    db = sqlconnect.connect()
    bluemac = db.cursor()
    sql = "delete from b_leak_m where asset_mac = '%s' and b_message = '%s'" % (mac,b_meassge)
    bluemac.execute(sql)
    db.commit()
    print '\ndelete b_leak_message', mac,
    db.close()

def deletewifileak(mac):
    db = sqlconnect.connect()
    bluemac = db.cursor()
    sql = "delete from router_leak where asset_mac = '%s'" % (mac)
    bluemac.execute(sql)
    db.commit()
    print '\ndelete b_leak_message', mac,
    db.close()



def paginate(page, size=5):
    """
    数据库 分页 和 翻页 功能函数
    @param page: int or str 页面页数
    @param size: int or str 分页大小
    @return: dict
    {
        'limit': 20,   所取数据行数
        'offset': 0,   跳过的行数
        'before': 0,   前一页页码
        'current': 1,  当前页页码
        'next': 2      后一页页码
    }
    """

    if not isinstance(page, int):
        try:
            page = int(page)
        except TypeError:
            page = 1

    if not isinstance(size, int):
        try:
            size = int(size)
        except TypeError:
            size = 20

    if page > 0:
        page -= 1

    data = {
        "limit": size,
        "offset": page * size,
        "before": page,
        "current": page + 1,
        "next": page + 2
    }

    return data


